cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES CXX)

enable_testing()

option(TEST_UTIL "Test Util" ON)
option(TEST_CRYPTO "Test crypto" ON)
option(TEST_NET "Test Net" ON)
option(TEST_NETSSL "Test NetSSL" ON)
option(TEST_SQLITE "Test Sqlite" ON)
option(TEST_ENCODINGS "Test Encodings" ON)
option(TEST_JWT "Test JWT" ON)
option(TEST_PROMETHEUS "Test Prometheus" ON)

set(POCO_COMPONENTS Foundation)
if(TEST_UTIL)
    list(APPEND POCO_COMPONENTS Util)
endif()
if(TEST_CRYPTO)
    list(APPEND POCO_COMPONENTS Crypto)
endif()
if(TEST_NET)
    list(APPEND POCO_COMPONENTS Net)
endif()
if(TEST_NETSSL)
    list(APPEND POCO_COMPONENTS NetSSL)
endif()
if(TEST_SQLITE)
    list(APPEND POCO_COMPONENTS DataSQLite)
endif()
if(TEST_ENCODINGS)
    list(APPEND POCO_COMPONENTS Encodings)
endif()
if(TEST_JWT)
    list(APPEND POCO_COMPONENTS JWT)
endif()
if(TEST_PROMETHEUS)
    list(APPEND POCO_COMPONENTS Prometheus)
endif()

find_package(Poco REQUIRED ${POCO_COMPONENTS} CONFIG)

if(Poco_VERSION VERSION_LESS "1.12.5")
    set(POCO_CXX_STD cxx_std_11)
else()
    set(POCO_CXX_STD cxx_std_14)
endif()

add_executable(core test_core.cpp)
target_link_libraries(core PRIVATE Poco::Foundation)
target_compile_features(core PRIVATE ${POCO_CXX_STD})

if(TEST_UTIL)
    add_executable(util test_util.cpp)
    target_link_libraries(util PRIVATE Poco::Util)
    target_compile_features(util PRIVATE ${POCO_CXX_STD})
    if(MINGW)
        target_link_options(util PRIVATE -municode)
    endif()
    add_test(NAME util COMMAND util)
endif()

if(TEST_CRYPTO)
    add_executable(tcrypto test_crypto.cpp)
    target_link_libraries(tcrypto PRIVATE Poco::Crypto)
    target_compile_features(tcrypto PRIVATE ${POCO_CXX_STD})
    add_test(NAME tcrypto COMMAND tcrypto ${CMAKE_CURRENT_SOURCE_DIR}/conanfile.py)
endif()

if(TEST_NET)
    add_executable(net test_net.cpp)
    target_link_libraries(net PRIVATE Poco::Net)
    target_compile_features(net PRIVATE ${POCO_CXX_STD})
    add_test(NAME net COMMAND net)

    if(TEST_UTIL)
        add_executable(net_2 test_net_2.cpp)
        target_link_libraries(net_2 PRIVATE Poco::Net Poco::Util)
        target_compile_features(net_2 PRIVATE ${POCO_CXX_STD})
        add_test(NAME net_2 COMMAND net_2)
    endif()
endif()

if(TEST_NETSSL)
    add_executable(netssl test_netssl.cpp)
    target_link_libraries(netssl PRIVATE Poco::NetSSL)
    target_compile_features(netssl PRIVATE ${POCO_CXX_STD})
    add_test(NAME netssl COMMAND netssl)
endif()

if(TEST_SQLITE)
    add_executable(sqlite test_sqlite.cpp)
    target_link_libraries(sqlite PRIVATE Poco::DataSQLite)
    target_compile_features(sqlite PRIVATE ${POCO_CXX_STD})
    add_test(NAME sqlite COMMAND sqlite)
endif()

if(TEST_ENCODINGS)
    add_executable(encodings test_encodings.cpp)
    target_link_libraries(encodings PRIVATE Poco::Encodings)
    target_compile_features(encodings PRIVATE ${POCO_CXX_STD})
    add_test(NAME encodings COMMAND encodings)
endif()

if(TEST_JWT)
    add_executable(jwt test_jwt.cpp)
    target_link_libraries(jwt PRIVATE Poco::JWT)
    target_compile_features(jwt PRIVATE ${POCO_CXX_STD})
    add_test(NAME jwt COMMAND jwt)
endif()

if(TEST_PROMETHEUS)
    add_executable(prometheus test_prometheus.cpp)
    target_link_libraries(prometheus PRIVATE Poco::Prometheus)
    target_compile_features(prometheus PRIVATE ${POCO_CXX_STD})
    add_test(NAME prometheus COMMAND prometheus)
endif()
